package controller

import (
	"context"
	pb "gitee.com/micro-services/protocol/hotel/api"
	log "github.com/sirupsen/logrus"
	"micro.hotel.client/ao"
	"micro.hotel.client/plugins"
)

type HotelController struct{}

func (h *HotelController) CreateHotel(ctx context.Context, r *pb.APICreateHotelReq, w *pb.APICreateHotelRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "CreateHotel")

	rsp, err := ao.CreateHotel(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.hotel.CreateHotel", md, r, w, rsp, err)
		return err
	}

	w.RetCode = rsp.RetCode
	w.Msg = rsp.Msg

	plugins.TracerFinish(span, "go.micro.api.hotel.CreateHotel", md, r, w)
	return nil
}

func (h *HotelController) DeleteHotel(ctx context.Context, r *pb.APIDeleteHotelReq, w *pb.APIDeleteHotelRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "DeleteHotel")

	rsp, err := ao.DeleteHotel(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.hotel.DeleteHotel", md, r, w, rsp, err)
		return err
	}

	w.RetCode = rsp.RetCode
	w.Msg = rsp.Msg

	plugins.TracerFinish(span, "go.micro.api.hotel.CreateHotel", md, r, w)
	return nil
}

func (h *HotelController) UpdateHotel(ctx context.Context, r *pb.APIUpdateHotelReq, w *pb.APIUpdateHotelRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "UpdateHotel")

	rsp, err := ao.UpdateHotel(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.hotel.UpdateHotel", md, r, w, rsp, err)
		return err
	}

	w.RetCode = rsp.RetCode
	w.Msg = rsp.Msg

	plugins.TracerFinish(span, "go.micro.api.hotel.UpdateHotel", md, r, w)
	return nil
}

func (h *HotelController) BatchHotelInfo(ctx context.Context, r *pb.APIBatchHotelInfoReq, w *pb.APIBatchHotelInfoRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "BatchHotelInfo")

	rsp, err := ao.BatchHotelInfo(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.hotel.BatchHotelInfo", md, r, w, rsp, err)
		return err
	}

	w.HotelInfo = rsp.HotelInfo

	plugins.TracerFinish(span, "go.micro.api.hotel.BatchHotelInfo", md, r, w)
	return nil
}

func (h *HotelController) SearchHotel(ctx context.Context, r *pb.APISearchHotelReq, w *pb.APISearchHotelRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "SearchHotel")

	rsp, err := ao.SearchHotel(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.hotel.SearchHotel", md, r, w, rsp, err)
		return err
	}

	w.HotelInfo = rsp.HotelInfo
	w.Count = rsp.Count

	plugins.TracerFinish(span, "go.micro.api.hotel.SearchHotel", md, r, w)
	return nil
}

func (h *HotelController) Recommend(ctx context.Context, r *pb.APIRecommendReq, w *pb.APIRecommendRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "Recommend")

	rsp, err := ao.Recommend(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.hotel.Recommend", md, r, w, rsp, err)
		return err
	}

	w.HotelInfo = rsp.HotelInfo
	w.Count = rsp.Count

	plugins.TracerFinish(span, "go.micro.api.hotel.Recommend", md, r, w)
	return nil
}
